package emu.decvices.cartridge.mappers;

import emu.decvices.cartridge.Cartridge;

import java.lang.reflect.Constructor;

/**
 * 映射器工厂
 *
 * @link https://www.nesdev.org/wiki/Mappermei
 */
public class MapperFactory {
    public static Mapper createMapper(RomHeader romHeader, Cartridge cartridge){
        String mapperName = romHeader.getMapper()+"";
        for(int i=mapperName.length();i<3;i++){
            mapperName="0"+mapperName;
        }
        mapperName = MapperFactory.class.getPackage().getName() + "." + "Mapper" + mapperName;
        try {
            Class cls = Class.forName(mapperName);
            Constructor c = cls.getDeclaredConstructor(Cartridge.class);
            return (Mapper)c.newInstance(cartridge);
        } catch (Exception e) {
            throw new RuntimeException("未实现的映射器类型:"+romHeader.getMapper());
        }
    }
}
